package edu.hawaii.duedates.wicket;

import java.io.File;
import java.util.List;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.duedates.configuration.ConfigurationManager;
import edu.hawaii.duedates.configuration.jaxb.User;


/**
 * 
 * 
 * @author John Ly, Scheller Sanchez
 */
public class Index extends DueDatesPage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** Stores the value for username. */
  private static String email = "";
  
  /** Stores the value for password. */
  private static String password = "";
  
  /**
   * Creates a single page where user enters username and password.
   * 
   */
  public Index() {
    
    add(HeaderContributor.forCss(edu.hawaii.duedates.wicket.Index.class, 
        "login.css", "screen"));
    
    if (getDueDates().getUser() == null) {
      // Create the form
      Form form = new Form("form");
      form.add(new TextField("Email", new PropertyModel(this, "email")));
      form.add(new PasswordTextField("Password", new PropertyModel(this, "password")));
      
      //add drop down box
      
      
      // Add the submit button
      form.add(new Button("Submit") {
        private static final long serialVersionUID = 1L;

        /** Submits the entered values. */
        @Override
        public void onSubmit() {   
          User logInUser = findUser(email, password);
          
          if (logInUser == null) {
            setErrorMessage("Invalid username or password provided.");
            setResponsePage(new Index());
          }
          else {
            getDueDatesSession().getDueDates().setUser(logInUser);
            setErrorMessage("");
            getDueDates().retrieveInfo();
            setResponsePage(new DisplayPage());        
          }
        }
      });
      add(new Label("failmessage", getErrorMessage()));
      add(form);    
    }
    else {
      setResponsePage(new DisplayPage());
    }
  }
  
  
  /**
   * Parses the XML and searches for the given user. 
   * Returns a User if a match is found.
   * @param email The email provided in the text field.
   * @param password The password provided in the text field.
   * @return The User account found in the XML.
   */
  public User findUser(String email, String password) {
    File defaultFile = new File(System.getProperty("user.home") + "/.duedates/duedates.xml");
    File testFile = new File(System.getProperty("user.dir") + "/xml/duedates.xml");
    // Use the testFile's location first
    ConfigurationManager manager = new ConfigurationManager(testFile);
    // Override if defaultFile location exists
    if (defaultFile.exists()) {
      manager = new ConfigurationManager();
    }
    List<User> listUsers = manager.getDueDates().getUsers().getUser();
    for (User person : listUsers) {
      if (person.getEmail().equals(email) && person.getPassword().equals(password)) {
        return person;
      }
    }
    return null;
  }
  
}
